package com.syqc.monitor.ui.cost

import androidx.lifecycle.MutableLiveData
import com.syqc.comlib.utils.GsonUtil
import com.syqc.entity.RepairInfoFee
import com.syqc.monitor.R
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil

class RepairViewModel : BaseFeeViewModel() {

    var feeWork = ""
        set(value) {
            field = value
            sum()
        }
    var feeManage = ""
        set(value) {
            field = value
            sum()
        }
    var feeParts = ""
        set(value) {
            field = value
            sum()
        }
    var feeOutSide = ""
        set(value) {
            field = value
            sum()
        }
    var feeOther = ""
        set(value) {
            field = value
            sum()
        }

    //维修人员
    var person = ""

    //驾驶员
    var driver = ""

    val feeTotalLive = MutableLiveData<String>()

    //提交时间
    var update = TimeUtil.instance.getNowTime(true)

    private var repairEntity: String = ""
    private var repairFee: String = ""

    override fun isParamsOk(): Boolean {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return false
        }
        if (
            feeWork.isEmpty() || feeManage.isEmpty() || feeParts.isEmpty() ||
            feeOutSide.isEmpty() || feeOther.isEmpty()
        ) {
            toast(R.string.hint_toast_fee)
            return false
        }

        val entity = RepairInfoFee(
            carId, update, remark,
            ToolsUtil.formatTo2F(feeTotal),
            feeWork, feeManage, feeOther,
            feeParts, feeOutSide, person = person, driverName = driver
        )
        repairFee = GsonUtil.toJson(entity)
        if ("$repairFee$photos" == repairEntity && isSuccess) {
            toast(R.string.toast_repeat_same_data)
            return false
        }
        repairEntity = "$repairFee$photos"
        return true
    }

    /**
     * 求和
     *
     */
    private fun sum() {
        feeTotal = getDouble(feeWork) + getDouble(feeManage) +
                getDouble(feeParts) + getDouble(feeOutSide) + getDouble(feeOther)
        feeTotalLive.postValue(ToolsUtil.formatTo2F(feeTotal))
    }

    /**
     * 提交维修
     *
     */
    fun uploadRepair() {
        if (!isParamsOk()) return
        uploadFee("FeeRepair", entity = repairFee.replace("%","%25"))
    }

    override fun clearData() {
        super.clearData()
        update = TimeUtil.instance.getNowTime(true)
    }

}